package homework.module3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class getMaxStr {

    /**
     * 获取给出的字符串的最长子串
     * @param targetStr
     * @param strLen:子字符串的长度
     * @return
     */
    public static List<String> getChileStr(String targetStr, int strLen){
        List<String> strList = new ArrayList<>();
        for (int i=0;i<targetStr.length();i++){
            if (i+strLen<targetStr.length()){
                String childStr = targetStr.substring(i,i+strLen);
                strList.add(childStr);
            }
        }
        return strList;
    }

    public static void main(String[] args) {
        String s1="asdafghjka";
        String s2="aaasdfg";

        List<String> manChildStrList = new ArrayList<>();   // 由于可能有长度相同的子串，用List记录最大子串
        String chileStr = s2;
        int childLen = s2.length();
        for (int i=0;i<s2.length();i++){
            if (childLen<1){
                break;
            }
            List<String> listChild = getChileStr(s2, childLen);  // 获取对应长度的子串List
            // 遍历数组判断是否有子串
            for (String child: listChild){
                if (s1.contains(child)){
                    manChildStrList.add(child);
                }
            }

            if (manChildStrList.size() >0){
                break;
            }
            childLen--;   // 子字符串的长度逐渐递减
        }

        // 输出最大子串
        for (String child: manChildStrList){
            System.out.println("最大的子串：" + child);
        }


    }

}
